//
//  CZWelcomViewController.swift
//  XLWB009
//
//  Created by apple on 3/26/16.
//  Copyright © 2016 apple. All rights reserved.
//

import UIKit

class CZWelcomViewController: UIViewController {

    //设置底部约束
    var iconViewBottom:NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        preraetUI()
    }
    
    //添加控件
    func preraetUI(){
    
        view.addSubview(imageView)
        view.addSubview(iconView)
        view.addSubview(Welcomlable)
        
        //设置约束
        imageView.translatesAutoresizingMaskIntoConstraints=false
        iconView.translatesAutoresizingMaskIntoConstraints=false
        Welcomlable.translatesAutoresizingMaskIntoConstraints=false
        
        //背景
        self.view.addConstraint(NSLayoutConstraint(item: imageView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        self.view.addConstraint(NSLayoutConstraint(item: imageView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        
        self.view.addConstraint(NSLayoutConstraint(item: imageView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem:self.view, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        
        self.view.addConstraint(NSLayoutConstraint(item: imageView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        //头像
        self.view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        iconViewBottom=(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
        self.view.addConstraint(iconViewBottom!)
        
         //欢迎界面
        self.view.addConstraint(NSLayoutConstraint(item: Welcomlable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        self.view.addConstraint(NSLayoutConstraint(item: Welcomlable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant:16))
    }
    
        //MARK动画
        override func viewDidAppear(animated: Bool) {
            
            super.viewDidAppear(animated)
            
           movaanimation()
        }
    
        //动画从下到上
       private func movaanimation(){
            UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
                
                self.iconViewBottom?.constant = -(UIScreen.mainScreen().bounds.height-160)
                //AutoLayout需要调用layoutIfNeeded，layoutIfNeeded会跟据约束重新设置fram
                self.view.layoutIfNeeded()
                
                }) { (_) -> Void in
                    
                UIView.animateWithDuration(CZanimateTime, animations: { () -> Void in
                
                self.Welcomlable.alpha=1
                
                }, completion: { (_) -> Void in
                    
                    print("动画完成")
                    // 切换界面
                    AppDelegate.outswitchRootViewController(CZMainTableViewController())
            })
        }
    }

         //MARK :懒加载
        //背景图片
    private lazy var imageView:UIImageView={
        
        let image=UIImageView(image: UIImage(named: "ad_background"))
        
        return image
    }()
    
    //头像
    private lazy var iconView:UIImageView={
    
        let icon=UIImageView(image: UIImage(named: "avatar_default_big"))
        icon.layer.cornerRadius=42
        icon.layer.masksToBounds=true
        
        return icon
        
    }()
    
    //文字
    private lazy var Welcomlable:UILabel={
    
        let lable=UILabel()
        
        lable.text="欢迎"
        lable.font=UIFont.systemFontOfSize(14)
        lable.sizeToFit()
        
        return lable
    }()
}
